package com.dantfb.util;

 
import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.ws.rs.core.MediaType;

import org.neo4j.graphdb.Node;
import org.neo4j.shell.util.json.JSONException;
import org.neo4j.shell.util.json.JSONObject;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
 
// TODO: Auto-generated Javadoc
/**
 * The Class RESTUtil.
 */
public class RESTUtil {

	/**
	 * Creates the node.
	 *
	 * @return the uri
	 */
	public static URI createNode(String type) {
		String query =  "CREATE (n:"+type+")";
		JSONObject jObject = new JSONObject();
		String SERVER_ROOT_URI = "http://localhost:7474/db/data/";
		final String nodeEntryPointUri = SERVER_ROOT_URI  + "node";
		 String cypherUri = SERVER_ROOT_URI + "cypher/";
            try {
				jObject.put("query", query);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		WebResource resource = Client.create().resource(nodeEntryPointUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).entity(jObject.toString())
				.post(ClientResponse.class);
		final URI location = response.getLocation();
		System.out.println(String.format(
				"POST to [%s], status code [%d], location header [%s]",
				nodeEntryPointUri, response.getStatus(), location.toString()));
		response.close();
		return location;
		
	}
	
	/**
	 * Adds the properties.
	 *
	 * @param node the node
	 * @param properties the properties
	 */
	public static void addProperties(URI node,HashMap<String,String> properties){
		for(Entry<String, String> entry : properties.entrySet()) {
		    addProperty( node, entry.getKey() , entry.getValue() );
		}
	 
	}
	
	/**
	 * Adds the property.
	 *
	 * @param node the node
	 * @param propertyName the property name
	 * @param propertyValue the property value
	 */
	private static void addProperty(URI node, String propertyName, String propertyValue) {
		String propertyUri = node.toString() + "/properties/" + propertyName;
		WebResource resource = Client.create()
		        .resource( propertyUri );
		ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
		        .type( MediaType.APPLICATION_JSON )
		        .entity( "\"" + propertyValue + "\"" )
		        .put( ClientResponse.class );

		System.out.println( String.format( "PUT to [%s], status code [%d]",
		        propertyUri, response.getStatus() ) );
		response.close();
		
	}
	
	/**
	 * Gets the data.
	 *
	 * @param params the params
	 * @param query the query
	 * @return the data
	 */
	public static JSONObject getData(HashMap<String,String> params,String query){
		JSONObject jObject = new JSONObject();
		String SERVER_ROOT_URI = "http://localhost:7474/db/data/";
		 String cypherUri = SERVER_ROOT_URI + "cypher/";
            try {
				jObject.put("query", query);
				jObject.put("params", params);
			} catch (JSONException e) {
				e.printStackTrace();
			}
            WebResource resource = Client.create()
                    .resource( cypherUri );
        
            ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
                    .type(MediaType.APPLICATION_JSON_TYPE)
                    .entity(jObject.toString())
                    .post(ClientResponse.class);
            try {
            	jObject = null;
				jObject = new JSONObject(response.getEntity(String.class).toString());
			} catch (ClientHandlerException e) {
				LogsUtil.getInstance().log(e, "RESTUtil",
						Thread.currentThread().getStackTrace()[1].getMethodName());
			} catch (UniformInterfaceException e) {
				LogsUtil.getInstance().log(e, "RESTUtil",
						Thread.currentThread().getStackTrace()[1].getMethodName());
			} catch (JSONException e) {
				LogsUtil.getInstance().log(e, "RESTUtil",
						Thread.currentThread().getStackTrace()[1].getMethodName());
			}
            
           return jObject;
	}
}
